package com.corpgovernment.organization.controller;

import com.corpgovernment.api.organization.model.org.request.GetUserConfigRequest;
import com.corpgovernment.api.organization.model.org.request.SaveUserConfigRequest;
import com.corpgovernment.api.organization.model.org.response.GetUserConfigResponse;
import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.organization.service.IUserConfigService;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 人员配置信息
 * @author xiaoxiaoguo
 */
@RestController
@RequestMapping("/userConfig")
@Slf4j
public class UserConfigController {

    @Autowired
    private IUserConfigService userConfigService;

    /**
     * 当前用户新增或修改用户委托配置
     * @param request
     * @param baseUserInfo
     * @return
     */
    @RequestMapping("/saveUserConfig")
    public JSONResult<Void> save(@RequestBody SaveUserConfigRequest request, BaseUserInfo baseUserInfo) {
        if(StringUtils.isBlank(request.getUserId())) {
            request.setUserId(baseUserInfo.getUid());
        }
        userConfigService.saveUserConfig(request);
        return JSONResult.ok();
    }

    /**
     * 当前用户查询委托配置
     * @param request
     * @return
     */
    @RequestMapping("/getUserConfig")
    public JSONResult<GetUserConfigResponse> get(@RequestBody GetUserConfigRequest request, BaseUserInfo baseUserInfo) {
        if(StringUtils.isBlank(request.getUserId())) {
            request.setUserId(baseUserInfo.getUid());
        }
        return JSONResult.success(userConfigService.getUserConfig(request));
    }

    /**
     * 根据用户IDs用户查询委托配置
     * @param userIdList
     * @return
     */
    @RequestMapping("/findUserConfigByUserIds")
    public JSONResult<List<GetUserConfigResponse>> findUserConfigByUserIds(@RequestParam("userIdList") List<String> userIdList) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return new JSONResult<>(null);
        }
        return JSONResult.success(userConfigService.findUserConfigByUserIds(userIdList));
    }

}
